import { eventBus } from '../utils/eventBus.js';
import { regeo } from '../utils/getPois.js';
import requestMsg from '../utils/requestMsg.js';

// 处理 ditance 事件
const handleDitance = (vm, res) => {
    try {
        vm.receiveDistance = res;
    } catch (error) {
        console.error('处理 ditance 事件时出错:', error);
    }
};

// 处理 toTarget_friend 和 toTarget_contacts 事件
const handleToTarget = (vm, item) => {
    try {
        vm.handerTarget(item);

    } catch (error) {
        console.error('处理 toTarget 事件时出错:', error);
    }
};

// 处理 user 事件
const handleUser = (vm, res) => {
    try {
        vm.user = res;
    } catch (error) {
        console.error('处理 user 事件时出错:', error);
    }
};

// 处理 newMessage 事件
const handleNewMessage = (vm, msg) => {
	 try {
        vm.handleWebSocketMessage(msg);
	
        vm.ajax.loading = false;
    } catch (error) {
        console.error('处理 newMessage 事件时出错:', error);
    }
};
const handisFly=(vm,res)=>{
	vm.stateConfig.isFly=res
console.log('-------',res)
}
// 处理 group_in 事件

// 处理 location 事件
const handleLocation = async (vm, res) => {
    try {
         vm.location = res;
		 if(!getApp().globalData.isFly){
		  const address= await regeo(res.longitude,res.latitude)
	     vm.pubilcData={...vm.pubilcData,...address}	 
		 }
	
	  		  
    } catch (error) {
        console.error('处理 location 事件时出错:', error);
    }
};

const handerToolShow=(vm,res)=>{

	vm.stateConfig.isTabBarShow=false
	vm.stateConfig.isToolBarShow=false	
	
	
}

// 处理 onlineStatusChanged 事件
const handleOnlineStatusChanged = (vm,res) => {
    try {
        vm.isOnline = res;
        if (res) {
            requestMsg(vm.toTarget, vm.lastTimes,vm.location);
            vm.ajax.loading = true;
        } else if (!res) { 
            getApp().getConnect();
        }

    } catch (error) {
        console.error('处理 onlineStatusChanged 事件时出错:', error);
    }
}
let settimeID = null;
const toolBarEven = (vm, res) => {
    // 清除之前的定时器
    if (settimeID) {
        clearTimeout(settimeID);
    }
    vm.stateConfig.isToolBarShow = true;
    vm.stateConfig.isTabBarShow = false;
    // 重新设置定时器
    settimeID = setTimeout(() => {
        vm.stateConfig.isToolBarShow = false;
    }, 5000);
};
const totarget =(vm,item)=>{
	vm.toTarget=item
}   

export const eventBusOn = (vm) => {
	// uni.$on('toTarget',(item)=>{totarget(vm,item)});
	// uni.$on('TouchMain',(res)=>{handerToolShow(vm,res)});
 //    uni.$on('ditance', (res) => handleDitance(vm, res));
 //    uni.$on('toTarget_friend', (item) => handleToTarget(vm, item));
 //    uni.$on('toTarget_contacts', (temp) => handleToTarget(vm, temp));
	// uni.$on('toolBarEven',(res)=>{toolBarEven(vm,res)})
 //    eventBus.$on('user', (res) => handleUser(vm, res));
 //    uni.$on('msg', (msg) => handleNewMessage(vm, msg));
 //   // uni.$on('location', (res) => handleLocation(vm, res));
 //    eventBus.$on('onlineStatusChanged', (res) => handleOnlineStatusChanged(vm, res));
	// uni.$on("isfly",(res)=>handisFly(vm,res))
};